<template>
  <div class="right-container flex-v flex-b g-card">
    <div class="card-brand">
      密码安全
      <div class="sub" style="margin-top: 10px">更改或查看密码。</div>
    </div>
    <div class="f1">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-position="top"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="password">
          <div class="rel" style="width: 290px;">
            <el-input
              v-model="ruleForm.password"
              class="transparent"
              :type="showPass ? 'text' : 'password'"
            />
            <img
              v-if="showPass"
              src="@/assets/images/eye.png"
              alt=""
              class="eye"
              @click="showPass = !showPass"
            >
            <img
              v-else
              src="@/assets/images/hide-eye.png"
              alt=""
              class="eye"
              @click="showPass = !showPass"
            >
          </div>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <div class="rel" style="width: 290px;">
            <el-input
              v-model="ruleForm.newPassword"
              class="transparent"
              :type="showNewPass ? 'text' : 'password'"
            />
            <img
              v-if="showNewPass"
              src="@/assets/images/eye.png"
              alt=""
              class="eye"
              @click="showNewPass = !showNewPass"
            >
            <img
              v-else
              src="@/assets/images/hide-eye.png"
              alt=""
              class="eye"
              @click="showNewPass = !showNewPass"
            >
          </div>
        </el-form-item>
      </el-form>
      <div class="flex-e align-center" style="margin-top: 50px;">
        <el-button
          type="primary"
          size="small"
          @click="submit"
        >保存更改</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { updatePassword } from '@/api/user'
export default {
  name: 'PersonalSetting',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        newPassword: ''
      },
      rules: {
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        newPassword: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      showPass: false,
      showNewPass: false
    }
  },
  methods: {
    submit() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await updatePassword(this.ruleForm)
          if (res.success) {
            this.$message.success('修改成功')
            this.ruleForm.password = ''
            this.ruleForm.newPassword = ''
          } else {
            this.$message.error(res.message)
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.eye {
  width: 24px;
  height: auto;
  cursor: pointer;
  position: absolute;
  z-index: 99;
  right: 20px;
  top: 9px;
}
.red {
  font-size: 14px;
  font-weight: 400;
  color: rgba(204, 37, 37, 1);
  margin-left: 20px;
  position: relative;
  top: 3px;
}
.del-icon {
  color: rgba(204, 37, 37, 1);
  right: 0;
  top: 0;
}
.id-card {
  width: 290px;
  height: 100px;
  border-radius: 15px;
  cursor: pointer;
}
.sub {
  font-size: 14px;
  font-weight: 400;
  color: rgba(113, 128, 150, 1);
}
.right-container {
  box-sizing: border-box;
  width: 370px !important;

  .userinfo {
    margin-bottom: 30px;
  }
  .card-brand {
    margin-bottom: 26px;
  }
  .grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-column-gap: 60px;
  }
  .avatar {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    margin-right: 24px;
  }
  .link {
    font-size: 20px;
    margin-left: 16px;
  }
  .btn-list {
    margin-top: 16px;
  }
  .transparent {
    width: 100%;
    max-width: 290px;
  }
}
</style>
